package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 银行账户 服务实现类
 * </p>
 *
 * @author jianghaijun
 * @since 2025-10-23
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {

    /**
     * 新增或更新银行账号信息接口
     * @param bankAccountUpsertReqDTO 银行账户信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateBankAccount(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        BankAccount bankAccount = BeanUtils.copyBean(bankAccountUpsertReqDTO, BankAccount.class);
        bankAccount.setCreateTime(LocalDateTime.now());
        bankAccount.setUpdateTime(LocalDateTime.now());
        saveOrUpdate(bankAccount);
    }

    /**
     * 获取当前用户银行账号
     * @return 银行账户信息
     */
    @Override
    public BankAccountResDTO currentUserBankAccount() {
        BankAccount bankAccount = baseMapper.selectOne(Wrappers.lambdaQuery(BankAccount.class)
                .eq(BankAccount::getId, UserContext.getCurrentUser().getId()));
        return BeanUtils.copyBean(bankAccount, BankAccountResDTO.class);
    }
}
